package lzj.jfenjoy;

import com.jfinal.kit.Kv;
import java.io.File;

/**
 * 代码生成器
 *
 * @author LuZhiJie
 */
public class JfGenerator {

  public static final JfGenerator me = new JfGenerator();
  protected final JfEnjoy jfEngine = new JfEnjoy();

  //    protected Kv tablemetaMap       = null;
  protected String packageBase = "top.jnaw.test";
  protected String srcFolder = "src";
  //    protected String viewFolder     = "WebRoot";
  protected String basePath = "controller";
  protected String basePathService = "service";
  protected boolean Alone = false;//是否按照表名进行独立的创建Controller

  public JfGenerator setPackageBase(String packageBase) {
    this.packageBase = packageBase;
    return this;
  }

  public JfGenerator setBasePath(String basePath) {
    this.basePath = basePath;
    return this;
  }

  public JfGenerator setSrcFolder(String srcFolder) {
    this.srcFolder = srcFolder;
    return this;
  }

  public JfGenerator setbasePathService(String basePathService) {
    this.basePathService = basePathService;
    return this;
  }
  public JfGenerator setAlone(boolean alone) {
    this.Alone = alone;
    if (this.Alone){
      this.basePathService = this.basePath;
    }
    return this;
  }
    
/*    public JfGenerator setViewFolder(String viewFolder){
        this.viewFolder = viewFolder;
        return this;
    }*/

  /**
   * java 代码生成
   */
  public JfGenerator javaRender(String className, String tableName) {
    return controller(className)/*.validator(className)*/.service(className, tableName);
  }

  private String toClassNameSmall(String className) {
    return new StringBuffer(className.substring(0, 1).toLowerCase()).append(className.substring(1))
        .toString();
  }

  private String toPackages(String className) {
    StringBuffer packages = new StringBuffer(packageBase).append(".").append(basePath);
    if (Alone){
      packages.append(".").append(className.toLowerCase());
    }
    return packages.toString();
  }
  private String toPackagesServer(String className) {
    StringBuffer packages = new StringBuffer(packageBase).append(".").append(basePathService);
    if (Alone){
      packages.append(".").append(className.toLowerCase());
    }
    return packages.toString();
  }

  /**
   * 生成数据库表
   */
/*    public JfGenerator tableSql(List<String> sqlList){
        DemoConfig config       = new DemoConfig();
        Plugins plugins         = new Plugins();
        config.configConstant(new Constants());
        config.configPlugin(plugins);
        for (IPlugin iPlugin: plugins.getPluginList())
            iPlugin.start();
        
        Db.batch(sqlList, 200);
        return this;
    }
    */

  /**
   * 生成Controller
   *
   * @param className 类名称
   */
  public JfGenerator controller(String className) {
    String packages = toPackages(className);

    String classNameSmall = toClassNameSmall(className);
    StringBuilder filepath = new StringBuilder()
        .append(System.getProperty("user.dir"))//当前项目绝对路径
        .append("/")
        .append(srcFolder)
        .append("/")
        .append(packages.replace(".", "/"))
        .append("/")
        .append(className)
        .append("Controller.java");
    File save_path = new File(filepath.toString());
//    System.out.println(filepath.toString());
    if (save_path.exists()) {
      System.err.println(className + "Controller.java文件已存在");
      return this;
    }
    String basePathService2 = basePathService;//引用Service的路径，并且是否独立进行判断
    if (Alone){
      basePathService2+="."+className.toLowerCase();
    }
    jfEngine.render("/controller.mb",
        Kv.by("package", packages)
            .set("className", className)
            .set("classNameSmall", classNameSmall)
            .set("basePath", basePath)
            .set("packageBase", packageBase)
            .set("basePathService", basePathService2)
        , filepath
    );
    return this;
  }

  /**
   * 生成Service
   *
   * @param className 类名称
   * @param tableName 表名
   */
  public JfGenerator service(String className, String tableName) {
    String packages = toPackagesServer(className);

    String classNameSmall = toClassNameSmall(className);
    StringBuilder filepath = new StringBuilder()
        .append(System.getProperty("user.dir"))
        .append("/")
        .append(srcFolder)
        .append("/")
        .append(packages.replace(".", "/"))
        .append("/")
        .append(className)
        .append("Service.java");
    File save_path = new File(filepath.toString());
    if (save_path.exists()) {
      System.err.println(className + "Service.java文件已存在");
      return this;
    }

    jfEngine.render("/service.mb",
        Kv.by("package", packages)
            .set("className", className)
            .set("classNameSmall", classNameSmall)
            .set("tableName", tableName)
            .set("packageBase", packageBase)
        , filepath);
    return this;
  }

/*
  protected class DataGenerator extends DataDictionaryGenerator {

    public DataGenerator(DataSource dataSource, String dataDictionaryOutputDir) {
      super(dataSource, dataDictionaryOutputDir);
    }

    public void rebuildColumnMetas(List<TableMeta> tableMetas) {
      super.rebuildColumnMetas(tableMetas);
    }
  }
*/

}


